/**
 *******************************************************************************
 * @file  usart/usart_uart_dma/source/main.c
 * @brief This example demonstrates UART data receive and transfer by DMA.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2022-03-31       CDT             First version
   2022-10-31       CDT             Delete the redundant code
                                    Read USART_DR.RDR when USART overrun error occur.
   2023-01-15       CDT             Update UART timeout function calculating formula for Timer0 CMP value
   2023-09-30       CDT             Split register USART_DR to USART_RDR and USART_TDR
   2024-11-08       CDT             Optimize function: USART_TxComplete_IrqCallback
                                    Add function: USART_StopTimeoutTimer
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2022-2025, Xiaohua Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by XHSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "main.h"
#include "stdio.h"
#include "string.h"
// #include "hc32_ll_icg.c"
#include "app_gpio.h"
#include "app_adc.h"
#include "app_uartDMA.h"
#include "FreeRTOS.h"
#include "task.h"

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/* Peripheral register WE/WP selection */
#define LL_PERIPH_SEL (LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | \
    LL_PERIPH_EFM | LL_PERIPH_SRAM)

//  //移植未用======================================
// static void SystemClockConfig(void)
// {
//     stc_clock_xtal_init_t stcXtalInit;

//     /* 仅初始化XTAL，不切换系统时钟源 */
//     // GPIO_AnalogCmd(BSP_XTAL_PORT, BSP_XTAL_PIN, ENABLE);
//     // (void)CLK_XtalStructInit(&stcXtalInit);
//     stcXtalInit.u8State = CLK_XTAL_ON;
//     stcXtalInit.u8Mode = CLK_XTAL_MD_OSC;
//     stcXtalInit.u8Drv = CLK_XTAL_DRV_ULOW;
//     stcXtalInit.u8StableTime = CLK_XTAL_STB_2MS;
//     (void)CLK_XtalInit(&stcXtalInit);
    
//     /* 删除 CLK_SetSysClockSrc(CLK_SYSCLK_SRC_XTAL); */
// }12
// 重定向中断向量表
void relocate_vector_table() {
    // 定义应用程序的起始地址11
    #define APPLICATION_ADDRESS 0x00008000
    // 设置中断向量表偏移寄存器
    SCB->VTOR = APPLICATION_ADDRESS;
}

 /// @brief 主函数入口
 /// @param  
 /// @return 
 int32_t main(void)
{
    // 重定向中断向量表
    relocate_vector_table();

    ///测试GPIOled
    // stc_gpio_init_t stcGpioInit;

    /* MCU Peripheral registers write unprotected */
    LL_PERIPH_WE(LL_PERIPH_SEL);

    /* Initialize BSP system clock. */
    BSP_CLK_Init();
    Flash_Init();

    // 存储数据
    // Flash_WriteU8(0,0x05);
    // Flash_WriteU8(1,my_data2);
    // Flash_WriteU8(2,my_data3);

    /* Initialize BSP expand IO. */
    BSP_IO_Init();

    /* Initialize UART.UART and DMA and TIME0  串口  DMA 跟定时器初始化  */
    App_uart_init();
    App_uart2_init();

    //A0的LED灯
    // GPIO_StructInit(&stcGpioInit);
    // GPIO_Init(GPIO_PORT_E, GPIO_PIN_00, &stcGpioInit);
    
    /* Configure USART RX/TX pin. */
    // Timer_Init();
    /* Enable peripheral clock */
  
    App_Tmr2_Init();
    // LL_PERIPH_WP(LL_PERIPH_SEL);

    /* Enable TX && RX && RX interrupt function */
    USART_FuncCmd(USART_UNIT, (USART_RX | USART_INT_RX | USART_RX_TIMEOUT | USART_INT_RX_TIMEOUT), ENABLE);
    USART_FuncCmd(USART_UNIT2,USART_TX ,ENABLE);

    //串口初始化
    App_init_gpio();
    // App_PB7_Init();
    // App_ExTPB7_Init();
    // debug_printf("Hello, World! This is a test message.\r\n");
   
    init_adc();    
    //DDL_DelayMS(1000);

	// return 0;
    // Set_LED_State(LED_STATUS_ON);
  //  debug_printf("Hello, World!4\r\n");
  LL_PERIPH_WP(LL_PERIPH_SEL);
    app_start();
    
    vTaskStartScheduler();	

    for (;;)
    {
        
    }
}



///////////////////////////////=========mainEND========///////////////////////////////////////////

/**
 * @}
 */

/**
 * @}
 */

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
